package com.bsb.biz.taglib;

import java.util.Collection;

import org.springframework.util.CollectionUtils;

import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.SysOrganization;
import com.bsb.base.model.SysResource;
import com.bsb.base.model.SysRole;
import com.bsb.biz.service.SysOrganizationService;
import com.bsb.biz.service.SysResourceService;
import com.bsb.biz.service.SysRoleDeptService;
import com.bsb.biz.service.SysRoleMenuService;
import com.bsb.biz.service.SysRoleService;
import com.bsb.biz.service.SysUserRoleService;
import com.bsb.biz.service.SysUserService;
import com.bsb.biz.spring.SpringUtils;

public class Functions {

    public static boolean in(Iterable iterable, Object element) {
        if(iterable == null) {
            return false;
        }
        return CollectionUtils.contains(iterable.iterator(), element);
    }

    public static String organizationName(Long organizationId) {
        SysOrganization organization = getOrganizationService().getSysOrganizationById(organizationId);
        if(organization == null) {
            return "";
        }
        return organization.getName();
    }

    public static String organizationNames(String organizationIds) {
        if(StringUtils.isEmpty(organizationIds)) {
            return "";
        }
        String[] split = organizationIds.split(",");
        StringBuilder s = new StringBuilder();
        for(String organizationId : split) {
        	SysOrganization organization = getOrganizationService().getSysOrganizationById(Long.valueOf(organizationId));
            if(organization == null) {
                return "";
            }
            s.append(organization.getName());
            s.append(",");
        }

        if(s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }
    public static String roleName(Long roleId) {
        SysRole role = getRoleService().getSysRoleById(roleId);
        if(role == null) {
            return "";
        }
        return role.getDescription();
    }

    public static String roleNames(String roleIds) {
        if(StringUtils.isEmpty(roleIds)) {
            return "";
        }
        String[] split = roleIds.split(",");
        StringBuilder s = new StringBuilder();
        for(String roleId : split) {
        	SysRole role = getRoleService().getSysRoleById(Long.valueOf(roleId));
            if(role == null) {
                return "";
            }
            s.append(role.getDescription());
            s.append(",");
        }

        if(s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }
    public static String resourceName(Long resourceId) {
        SysResource resource = getResourceService().getSysResourceById(resourceId);
        if(resource == null) {
            return "";
        }
        return resource.getName();
    }
    public static String resourceNames(String resourceIds) {
        if(StringUtils.isEmpty(resourceIds)) {
            return "";
        }
        String[] split = resourceIds.split(",");
        StringBuilder s = new StringBuilder();
        for(String resourceId : split) {
        	SysResource resource = getResourceService().getSysResourceById(Long.valueOf(resourceId));
            if(resource == null) {
                return "";
            }
            s.append(resource.getName());
            s.append(",");
        }

        if(s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }

    private static SysUserService userService;
    private static SysRoleService roleService;
    private static SysResourceService resourceService;
    private static SysOrganizationService organizationService;
    private static SysUserRoleService userRoleService;
    private static SysRoleMenuService roleMenuService;
    private static SysRoleDeptService roleDeptService;

    public static SysOrganizationService getOrganizationService() {
        if(organizationService == null) {
            organizationService = SpringUtils.getBean(SysOrganizationService.class);
        }
        return organizationService;
    }

    public static SysRoleService getRoleService() {
        if(roleService == null) {
            roleService = SpringUtils.getBean(SysRoleService.class);
        }
        return roleService;
    }

    public static SysResourceService getResourceService() {
        if(resourceService == null) {
            resourceService = SpringUtils.getBean(SysResourceService.class);
        }
        return resourceService;
    }
}